<?php

namespace App\Library\Traits\Orm;

use App\Library\Logic\AuctionLogic;
use App\Models\AfterSaleModel;
use App\Models\AuctionCertificationModel;
use App\Models\AuctionModel;
use App\Models\LogisticsModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

trait AuctionOrderOrmTrait
{
    public function afterSale(): BelongsTo
    {
        return $this->belongsTo(AfterSaleModel::class, 'id', 'object_id')
            ->where('object_type', $this->table)->orderByDesc('id');
    }

    public function logistics(): BelongsTo
    {
        return $this->belongsTo(LogisticsModel::class, 'id', 'object_id')->orderByDesc('id')->where('object_type', 0);
    }

    public function auctionInfo(): BelongsTo
    {
        return $this->belongsTo(AuctionModel::class, 'auction_id', 'id');
    }

    public function certificationInfo(): BelongsTo
    {
        return $this->belongsTo(AuctionCertificationModel::class, 'seller_id', 'user_id');
    }

    public function auction(): BelongsTo
    {
        return $this->belongsTo(AuctionLogic::class, 'auction_id', 'id')
            ->leftJoin('shop_category as categoryOne', 'categoryOne.category_id', 'category_id_one')
            ->leftJoin('shop_category as categoryTwo', 'categoryTwo.category_id', 'category_id_two')
            ->leftJoin('shop_category as categoryThree', 'categoryThree.category_id', 'category_id_three')
            ->select([
                'serial_number as auction_number',
                'id',
                'bail_price',
                'categoryOne.category_name as category_one_name',
                'categoryTwo.category_name as category_two_name',
                'categoryThree.category_name as category_three_name'
            ]);
    }

    public function storeInfo(): BelongsTo
    {
        return self::belongsTo(AuctionCertificationModel::class, 'seller_id', 'user_id');
    }
}
